// pages/personal/personal.js
var util = require('../../utils/util.js');
var app = getApp()
Page({
    data: {
        userInfo: {},
        hasEmptyGrid: false,
        showModalStatus: false,
        statuss: false,
        dataLists: [1, 2, 3, 4, 5, 6, 7, 8],
        daystatus: [0, 0, 0, 0]
            // seconds:[]
    },
    bindViewTap: function() {
        // wx.navigateTo({
        //     url: '../logs/logs'
        // })
    },
    onLoad: function() {

        const date = new Date();
        const cur_year = date.getFullYear();
        const cur_month = date.getMonth() + 1;
        const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
        this.calculateEmptyGrids(cur_year, cur_month);
        this.calculateDays(cur_year, cur_month);
        this.setData({
            cur_year,
            cur_month,
            weeks_ch
        })
        this.getList();
        this.getMonthList();
    },
    getThisMonthDays(year, month) {
        return new Date(year, month, 0).getDate();
    },
    getFirstDayOfWeek(year, month) {
        return new Date(Date.UTC(year, month - 1, 1)).getDay();
    },
    calculateEmptyGrids(year, month) {
        const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
        // console.log(firstDayOfWeek)
        let empytGrids = [];
        if (firstDayOfWeek > 0) {
            for (let i = 0; i < firstDayOfWeek; i++) {
                empytGrids.push(i);
            }
            this.setData({
                hasEmptyGrid: true,
                empytGrids
            });
        } else {
            this.setData({
                hasEmptyGrid: false,
                empytGrids: []
            });
        }
    },
    calculateDays(year, month) {
        let days = [];

        const thisMonthDays = this.getThisMonthDays(year, month);

        for (let i = 1; i <= thisMonthDays; i++) {
            days.push(i);
        }

        this.setData({
            days
        });
    },
    // 获取当月打卡信息
    getMonthList: function() {
        var month = 0;
        if (this.data.cur_month <= 9) {
            month = '0' + this.data.cur_month;
        } else {
            month = this.data.cur_month;
        }
        var arr = {
            year: this.data.cur_year,
            month: month
        }
        util.myrequest(arr, '/checkin/lists', 'GET', (res) => {
            if (res.result) {
                var days = this.data.days; //当月天数数据
                var getdays = res.datas; //后台获取当月打卡记录
                var datas = days; //默认数据
                // 打卡状态如果有记录则插入否者不变根据插入的状态显示
                for (var i = 1; i <= days.length; i++) {
                    for (var j = 0; j < getdays.length; j++) {
                        if (getdays[j][0] == days[i]) {
                            datas[i] = getdays[j][1];
                        }
                    }
                }
            }
            this.setData({
                datalist: datas
            })
        })
    },
    handleCalendar(e) {
        const handle = e.currentTarget.dataset.handle;
        const cur_year = this.data.cur_year;
        const cur_month = this.data.cur_month;
        if (handle === 'prev') {
            let newMonth = cur_month - 1;
            let newYear = cur_year;
            if (newMonth < 1) {
                newYear = cur_year - 1;
                newMonth = 12;
            }

            this.calculateDays(newYear, newMonth);
            this.calculateEmptyGrids(newYear, newMonth);

            this.setData({
                cur_year: newYear,
                cur_month: newMonth
            })

        } else {
            let newMonth = cur_month + 1;

            let newYear = cur_year;
            if (newMonth > 12) {
                newYear = cur_year + 1;
                newMonth = 1;
            }

            this.calculateDays(newYear, newMonth);
            this.calculateEmptyGrids(newYear, newMonth);

            this.setData({
                cur_year: newYear,
                cur_month: newMonth
            })
        }
        this.getMonthList();
    },
    curentdata: function(e) {

    },
    //员工详情
    getList: function() {

        util.myrequest({ 'id': '1' }, '/staff/info', 'GET', (res) => {
            // console.log(res)
            // let arr = this.data.seconds.concat(res.datas);
            this.setData({
              name: res.datas.name + '\n',
                job: res.datas.job + '\n',
                grade: res.datas.grade + '\n',
                phone: res.datas.phone + '\n',
                thumb: res.datas.thumb,
            })
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    showss: function() {
        this.setData({
            statuss: false
        })
    },
    //  查看打卡记录
    powerDrawer: function(e) {
        // console.log(this.data.dataLists.indexOf(2))
        this.setData({
            statuss: true
        })

        var currentData = e.currentTarget.dataset.currentdata;
        // console.log(currentData)
        var month = 0;
        if (this.data.cur_month <= 9) {
            month = '0' + this.data.cur_month;
        } else {
            month = this.data.cur_month;
        }
        var arr = {
            year: this.data.cur_year,
            month: month,
            day: currentData
        }
        util.myrequest(arr, '/checkin/lists', 'GET', (res) => {
                var data = res.datas;
                console.log(res.datas)
                var sky = [0, 0, 0, 0];
                if (data.length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        sky[i] = data[i].status;
                    }
                    this.setData({
                        daystatus: sky
                    })
                    console.log(sky)
                } else {
                    this.setData({
                        daystatus: sky
                    })
                }

            })
            // console.log(this.data.cur_year + '-' + month + '-' + currentData)
    },
    util: function(currentStatu) {
        /* 动画部分 */
        // 第1步：创建动画实例   
        var animation = wx.createAnimation({
            duration: 200, //动画时长  
            timingFunction: "linear", //线性  
            delay: 0 //0则不延迟  
        });

        // 第2步：这个动画实例赋给当前的动画实例  
        this.animation = animation;

        // 第3步：执行第一组动画  
        animation.opacity(0).rotateX(-100).step();

        // 第4步：导出动画对象赋给数据对象储存  
        this.setData({
            animationData: animation.export()
        })

        // 第5步：设置定时器到指定时候后，执行第二组动画  
        setTimeout(function() {
            // 执行第二组动画  
            animation.opacity(1).rotateX(0).step();
            // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
            this.setData({
                animationData: animation
            })

            //关闭  
            if (currentStatu == "close") {
                this.setData({
                    showModalStatus: false
                });
            }
        }.bind(this), 200)

        // 显示  
        if (currentStatu == "open") {
            this.setData({
                showModalStatus: true

            });

        }
    }

})